package com.ruoyi.module.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.page.PageFactory;
import com.ruoyi.common.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.ruoyi.module.app.mapper.TestMapper;
import com.ruoyi.module.app.domain.Test;
import com.ruoyi.module.app.service.ITestService;

import java.util.List;

/**
 * 系统测试Service业务层处理
 *
 * @author mars
 * @date 2022-07-23
 */
@Slf4j
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {

    @Override
    public Test add(Test param) {
        super.save(param);
        return param;
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    /**
     * 批量删除
     *
     * @author value
     * @Date 2019-02-23
     */
    public boolean deleteBatch(List<Long> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public boolean update(Test param) {
        return this.updateById(param);
    }

    @Override
    public Test findById(Long id) {
        return this.getById(id);
    }

    @Override
    @SuppressWarnings("all")
    public PageInfo findPageBySpec(Test param) {
        Page pageContext = getPageContext();
        LambdaQueryWrapper<Test> query = this.buildWrapper(param);
        log.info("查询参数：{}", JSONObject.toJSONString(query));
        IPage page = this.page(pageContext, query);
        log.info("查询返回结果：{}", JSONObject.toJSONString(page));
        return PageFactory.buildPageInfo(page);
    }

    /**
     * 构建查询条件
     *
     * @author mars
     * @Date 2022-07-23
     */
    private LambdaQueryWrapper<Test> buildWrapper(Test param) {
        LambdaQueryWrapper<Test> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getName())){
               query.like(Test::getName ,param.getName());
        }
        if (param.getAge() != null){
               query.eq(Test::getAge ,param.getAge());
        }
        return query;
    }


    private Page<Object> getPageContext() {
        return PageFactory.buildPage();
    }
}
